<template>
  <div>
    <Headline
      :headline1="$t('m.AccessAdmin') + '  /  '"
      :headline2="$t('m.TurnoverConditionAnalyze')"
    />
    <a-card style="width: 100%">
      <select-tree
        @checkData="treeChange"
        style="margin-right: 15px; width: 150px"
      >
      </select-tree>
      {{ $t("m.departureTime") }}
      <a-date-picker
        style="margin-right: 15px"
        show-time
        @change="leaveChange"
        placeholder="Select Time"
      />
      {{ $t("m.returnTime") }}
      <a-date-picker
        style="margin-right: 15px"
        show-time
        @change="backChange"
        placeholder="Select Time"
      />
      <a-input-search
        :placeholder="$t('m.PleaseEnterYourSearchTerms')"
        v-model="forminitial.key"
        style="width: 200px; margin-right: 15px"
        enter-button
        @search="onSearch"
      />
      <a-checkbox @change="onChange" style="margin-right: 15px">
        含子节点
      </a-checkbox>
      <a-dropdown>
        <a-button type="primary" icon="unordered-list">
          {{ $t("m.More") }}
        </a-button>
        <a-menu slot="overlay">
          <a-menu-item @click="exportList">
            <a href="javascript:;">{{ $t("m.ExportExcel") }}</a>
          </a-menu-item>
          <a-menu-item @click="manual">
            <a href="javascript:;">{{ $t("m.ManualAnalysis") }}</a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
      <a-table
        style="margin-top: 30px"
        :columns="columns"
        :rowKey="(record) => record.id"
        :data-source="data"
        :pagination="false"
        bordered
      />
      <a-pagination
        class="pages"
        show-size-changer
        :default-current="1"
        :total="total"
        @change="accPages"
        :pageSizeOptions="['5', '10', '30', '40']"
        @showSizeChange="accShowSizeChange"
      />
    </a-card>
    <a-modal
      :title="$t('m.DataAnalysis')"
      :visible="visible"
     :cancelText="$t('m.Cancel')"
      :okText="$t('m.Ok')"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-date-picker @change="dataChange" />
    </a-modal>
  </div>
</template>

<script>
import Headline from "@/components/Headline";
import SelectTree from "@/components/common/SelectTree";
import { getPerages } from "@/api/keraStase";
import {
  getLeaveAnalysisList,
  exportLoginin,
  analysisDaily,
} from "@/api/faceRecognition";
export default {
  name: "AccessTurnover",
  components: { Headline, SelectTree },
  data() {
    return {
      forminitial: {
        ymOrgTreeCode: "",
        leaveTime: "",
        backTime: "",
        key: "",
        viewChild: false,
        start: 0,
        limit: 10,
        page: 1,
      },
      total: 0,
      demotime: "",
      data: [],
      columns: [
        { title: this.$t("m.PersonID"), dataIndex: "stafforstudinfoid" },
        { title: this.$t("m.PersonName"), dataIndex: "stafforstuname" },
        {
          title: this.$t("m.Sex"),
          dataIndex: "sex",
          customRender: function (text) {
            if (text == "1") {
              return "男";
            } else {
              return "女";
            }
          },
        },
        {
          title: this.$t("m.Status"),
          dataIndex: "direction",
          customRender: function (text) {
            if (text == "1") {
              return "正常";
            } else if (text == "2") {
              return "未归";
            } else {
              return "迟到";
            }
          },
        },
        { title: this.$t("m.departureTime"), dataIndex: "leavetime" },
        { title: this.$t("m.returnTime"), dataIndex: "backtime" },
        { title: this.$t("m.LastReturnTime"), dataIndex: "maxbacktime" },
        { title: this.$t("m.department"), dataIndex: "custorgname" },
      ],
      visible: false,
      manualTime: "",
    };
  },
  created() {
    // getPerages({ params: { orgType: 1 } })
    //         .then((res) => {
    //           this.forminitial.ymOrgTreeCode = res.data[0].ymorgtreecode
    //           this.getLeaveisList()
    //           console.log(res.data[0].ymorgtreecode)

    //         })
    //         .catch((err) => {
    //           this.$message.error("获取部门数据失败");
    //         });
    this.getLeaveisList();
  },
  beforeCreate() {},
  methods: {
    handleOk() {
      if (this.manualTime == "") return this.$message.warning("请选择时间");
      analysisDaily({ daily: this.manualTime })
        .then((res) => {
          if (res.data.success == true) {
            this.$message.success(res.data.msg);
          }
          if (res.data.success == false) {
            this.$message.warning(res.data.msg);
          }
        })
        .catch((err) => {
          this.$message.error("操作失败");
        });
      this.visible = false;
    },
    dataChange(e) {
      this.manualTime = this.$moment(e._d).format("YYYY-MM-DD hh:mm:ss");

      console.log(e._d);
    },
    manual() {
      console.log("a");
      this.visible = true;
    },
    handleCancel() {
      this.visible = false;
    },
    treeChange(e) {
      this.forminitial.ymOrgTreeCode = e.ymorgtreecode;
      //           this.getLeaveisList()
      console.log(e);
    },
    backChange(e) {
      this.forminitial.backTime = this.$moment(e._d).format(
        "YYYY-MM-DD hh:mm:ss"
      );
      this.getLeaveisList();
    },
    leaveChange(e) {
      console.log(this.$moment(e._d).format("YYYY-MM-DD hh:mm:ss"));
      this.forminitial.leaveTime = this.$moment(e._d).format(
        "YYYY-MM-DD hh:mm:ss"
      );
      this.getLeaveisList();
    },
    exportList() {
      exportLoginin(this.forminitial.ymOrgTreeCode)
        .then((res) => {
          if (res.data.success == true) {
            this.$message.success("导入成功");
            location.href = this.$store.state.BaseUrl + res.data.msg;
          }
          if (res.data.success == false) {
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getLeaveisList() {
      getLeaveAnalysisList({ params: this.forminitial })
        .then((res) => {
          this.data = res.data.data;
          this.total = res.data.total;
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onSearch() {
      this.getLeaveisList();
    },
    onChange(e) {
      this.forminitial.viewChild = e.target.checked;
      this.getLeaveisList();
      console.log(e.target.checked);
    },
    //  //分页
    accShowSizeChange(page, limit) {
      this.forminitial.page = page;
      this.forminitial.limit = limit;
      this.forminitial.start = (page - 1) * limit;
      this.getLeaveisList();
    },
    accPages(page, pageSize) {
      this.forminitial.page = page;
      this.forminitial.limit = pageSize;
      this.forminitial.start = (page - 1) * pageSize;
      this.getLeaveisList();
    },
  },
};
</script>

<style>
</style>